import processing.core.PImage;

class TextBox extends View
{
  ////////////////////////////////////////////////////////////////////////////////////////////////////
  private String text;
  private boolean  activated = false;
  // public Timer ticktock; // Timer used for blinking cursor
  private boolean timer = true;
  private int lastsec = 0;
  private int cursorPos = 0;
  boolean firstLetter = true;
  private String defaultText;
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////
  public TextBox(float x_, float y_, float w_, float h_,MainView p, String d)
  {
    super(x_, y_, w_, h_, p);
    text = "";
    this.defaultText = d;
    // tickTock = new Timer();
  }
  public String getText(){
	  return text;
  }
  public boolean toggleActivated(){
	  activated = !activated;
	  return activated;
  }
  public boolean getActivated(){
	  return activated;
  }
  public void setActivated(boolean a){
	  activated = a;
  }
  ////////////////////////////////////////////////////////////////////////////////////////////////////
  
  void drawContent()
  {

    parent.fill(24,116,205);
    if (activated)
    	parent.stroke(135,206,235);
    else

    	parent.stroke(24,116,205);
    parent.strokeWeight((float) 1.5);
    parent.rect(0, 0, w, h);

    parent.fill(0);
    parent.textSize(12);
    parent.textAlign(parent.LEFT, parent.CENTER);
   
    if (text.equals("") && !activated)
    {
    	parent.fill(255);
    	parent.text(this.defaultText, 15, 7);
    }
    else {
      /*  if (activated)
       fill(0);
       else
       fill(#A3A3A3);
       */
      if (timer && activated){
      String pre = text.substring(0,cursorPos);
      pre+="|";
      pre += text.substring(cursorPos);
      parent.fill(255);
      parent.text(pre, 15, 7);
      
    }
      else{
    	  parent.fill(255);
    	  parent.text(text, 15, 7);
      }
    }
    //textFont(fbold);
    parent.textAlign(parent.CENTER,parent.CENTER);
    //System.out.println(parent.second());

    if (parent.second()%1 <= 0.5 && parent.second()!=lastsec) {
      timer = !timer;
      //System.out.println("Toggling "+ timer);
      lastsec = parent.second();
    }
    parent.noStroke();
    parent.textAlign(parent.LEFT, parent.BASELINE);
  }
  ////////////////////////////////////////////////////////////////////////////////////////////////////
  public void keyPressed(char key) 
  {
    //System.out.println("Key = "+key + " Text = "+text );
    if (activated) {
    //	System.out.println("Key = "+ key);
      if (key == 8)
      {
    	  //System.out.println("Cursor Pos = "+ cursorPos);
        if (text.length() > 0 && cursorPos > 0) {
        	String text2 = text.substring(cursorPos);
        	text = text.substring(0, cursorPos - 1);
        	text = text.concat(text2);
        	//if(cursorPos > 1)
        	cursorPos--;
        	}
        
      }
      else
      {
        if (key == parent.CODED) {
          if (parent.keyCode == parent.RIGHT) {
            if(cursorPos < text.length())
            cursorPos++;
            
          }
          if(parent.keyCode == parent.LEFT){
            if(cursorPos > 0)
            cursorPos--;
          
          }
        }
        else {
        	
          if (key != 65535 && key != 10 && key != 27) {
        	  //System.out.println("Key = "+ key);
            if (cursorPos == text.length()) {
              text += key;
              cursorPos++;
              firstLetter  =false;
            }else{
            	 String s2 = "";
                 s2 += text.substring(0, cursorPos);
                 s2+=key;
                 s2+= text.substring(cursorPos);
                 text = s2;
                 firstLetter  =false;
            	
            }
        //    else
          //  {
            //	if(key != '\n'){
            		
             
            //	}
            //}
          }
        }
      }
    }
    //System.out.println("Key = "+key + " Text = "+text );
    //   System.out.println("Text in there: "+ text);
//    return activated;
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////
  // Private stuff.
 // PImage icon;
  //String label;
  /////////////////////////////////////////////////////////////////////////////////////////////////////
  boolean contentPressed(float lx, float ly)
  {

    activated = !activated;
    if(activated && firstLetter)
    	text = "";
    	
    //filterKeyInput = activated;
    // System.out.println("Activated = "+ activated);
    return true;
  }
  /* void mouseClicked()
   { 
   
   if (!ptInRect(px, py, x,y, w, h)) { 
   
   activated = false;
   System.out.println("Activated = "+ activated);
   
   return;
   }
   float lx = px - x;
   float ly = py - y;
   // check our subviews first
   for (int i = subviews.size()-1; i >= 0; i--) {
   View v = (View)subviews.get(i);
   if (v.mousePressed(lx, ly)) return true;
   }
   contentPressed(lx,ly);
   }
   */

boolean mousePressed(float px, float py)
{
	//System.out.println("Should I deactivated "+ this.defaultText+"? It's "+this.activated);
	 if (!ptInRect(px, py, x, y, w, h)) {
		 
		this.setActivated(false);
		
		 return false;}
	    float lx = px - x;
	    float ly = py - y;
	    // check our subviews first
	    for (int i = subviews.size()-1; i >= 0; i--) {
	      View v = (View)subviews.get(i);
	      
	      if (v.mousePressed(lx, ly)){ 
	    	  
	    	  return true;
	      }
	    }
	    return contentPressed(lx, ly);	    
}

}
